package com.example.mq.mqServer.core.exchange;

import com.example.mq.mqServer.util.JsonUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: 0.mq
 * @BelongsPackage: com.example.mq.mqServer.core
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-22  12:03
 * @Version : 1.0
 * @Description : MQ交换机
 */
@Data
public class Exchange {
    //交换机名称，唯一标识
    private String name;
    //交换机类型
    private ExchangeType type;
    //交换机是否持久化,true是，false否
    private boolean durable;
    //自动删除，如果当前交换机无人使用，就自动删除。（此功能暂无实现）
    private boolean autoDelete;
    //创建交换机时，一些额外的参数选项（此功能暂无实现）
    private Map<String,Object> arguments;


    /*------------------------------------普--通--方--法------------------------------------------------------------------
     * @Desc  : 构造方法
     * @Param : []
     * @Return:
    **/
    public Exchange() {
        this.name = "未命名";
        this.type = ExchangeType.DIRECT;
        this.durable = false;
        this.autoDelete = false;
        this.arguments = new HashMap<>();
    }

    public Exchange(String name, ExchangeType type, boolean durable) {
        this.name = name;
        this.type = type;
        this.durable = durable;
        this.autoDelete = false;
        this.arguments = new HashMap<>();
    }

    public Exchange(String name, ExchangeType type, boolean durable, boolean autoDelete, Map<String, Object> arguments) {
        this.name = name;
        this.type = type;
        this.durable = durable;
        this.autoDelete = autoDelete;
        this.arguments = arguments;
    }

    //json与map的互转
    public String getArguments()  {
        try {
            return JsonUtils.getJson(arguments);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void setArguments(String arguments)  {
        try {
            this.arguments = JsonUtils.setSrc(arguments, new TypeReference<HashMap<String, Object>>(){});
            return;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        this.arguments = null;
    }

    public Object getArgumentsValue(String key){
        return arguments.get(key);
    }

    public void setArgumentsKey_Value(String key,Object value){
        arguments.put(key, value);
    }





}
